package com.dlmz.view;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.dlmz.util.BaseDao;


@SuppressWarnings("serial")
public class RegistJframe extends JFrame {
	JLabel jl1,jl2,jl3,jl4;
	JTextField jtfField1,jtfField2,jtfField3;
	JButton jb,jb2;
	public RegistJframe() {
		// TODO Auto-generated constructor stub
		init();
		listener();
	}
	private void init() {
		// TODO Auto-generated method stub
		jl1=new JLabel("工号:");
		jl2=new JLabel("密 码:");
		jl4=new JLabel("确认密码:");
		//设置jl1的位置
		jl1.setBounds(100,80, 80, 30);
		//设置jl2的位置
		jl2.setBounds(100,120, 100, 30);
		jl4.setBounds(100,160, 100, 30);
		//设置jl1的字体
		jl1.setFont(new Font("华文行楷", Font.BOLD, 20));
		//设置jl2的字体
		jl2.setFont(new Font("华文行楷", Font.BOLD, 20));
		jl4.setFont(new Font("华文行楷", Font.BOLD, 20));
		jtfField1=new JTextField();              //工号
		jtfField1.setBounds(200,80,130,30);
		
		jtfField2=new JTextField();				//密码
		jtfField2.setBounds(200,120,130,30);
		
		jtfField3=new JTextField();				//确认密码
		jtfField3.setBounds(200,160,130,30);
		
		jb=new JButton("注册");
		jb2=new JButton("重置");
		jb.setBounds(130, 220, 60, 30);
		jb2.setBounds(230, 220, 60, 30);
		this.setLayout(null);
		//将组件添加到窗口中
		this.add(jl1);
		this.add(jtfField1);
		this.add(jl2); 
		this.add(jtfField2);
		this.add(jl4); 
		this.add(jtfField3);
		this.add(jb);
		this.add(jb2);
		//this.add(jl3);
		this.setTitle("注册界面");
		this.setSize(400,370);
		this.setLayout(null);
		//设置窗口居中
		this.setLocationRelativeTo(null);
		//设置窗口是否可见
		this.setVisible(true);
	}
	private void listener() {
		// TODO Auto-generated method stub
		jb.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String username=jtfField1.getText().trim();
				String pwd=jtfField2.getText().trim();
				String comfirm=jtfField3.getText().trim();
				//创建增加的sql语句
				String sql="insert into user(Account,Passwd) values(?,?)";
				//创建BaseDao
				if(username.equals("")||pwd.equals("")) {
					JOptionPane.showMessageDialog(null, "工号或密码未填写！");	
					return;
				}
				else if (!pwd.equals(comfirm)) {
					JOptionPane.showMessageDialog(null, "两次密码输入不相同！");	
					return;
				}
				else {
					BaseDao baseDao=new BaseDao(); 
					Integer number=baseDao.update(sql,username,pwd); 
					if(number>0) {
						JOptionPane.showMessageDialog(null, "注册成功"); 
						dispose();
						new LoginJframe();               //成功跳回到登录界面
					}else {
						JOptionPane.showMessageDialog(null, "注册失败"); 
					}
				}
			}
		});
		jb2.addActionListener(new ActionListener() { //重置
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jtfField1.setText("");
				jtfField2.setText("");
				jtfField3.setText("");
			}
		});
		this.addWindowListener(new WindowAdapter() {//退出前的确认
			public void windowClosing(WindowEvent e) {
				if (JOptionPane.showConfirmDialog(RegistJframe.this, "是否退出注册界面？")==JOptionPane.OK_OPTION) {
					dispose();
					new LoginJframe();
				}else {
					new RegistJframe();
				}
			}
		});
	}

}
